const fs = require("fs");
const path = require("path");

module.exports = (devServer) => {
  // console.log(devServer)
  if (!devServer) {
    throw new Error("webpack-dev-server is not defined");
  }
  devServer.app.use(/\/mock/, (req, res) => {
    const jsonPath = `${path.join(
      __dirname,
      req.path.replace(/^\/mock/, ""),
    )}.json`;
    // console.log(1234, jsonPath)
    const content = fs.readFileSync(jsonPath, "utf8");
    // console.log(123, content)
    res.json(JSON.parse(content));
  });
};
